//package com.usefullc.crawler.selenium;
//
//import com.usefullc.common.enums.WebDriverTypeEnum;
//import org.apache.commons.lang3.StringUtils;
//import org.openqa.selenium.JavascriptExecutor;
//import org.openqa.selenium.WebDriver;
//import org.openqa.selenium.chrome.ChromeDriver;
//import org.openqa.selenium.chrome.ChromeOptions;
//import org.openqa.selenium.firefox.FirefoxDriver;
//import org.openqa.selenium.firefox.FirefoxOptions;
//import org.openqa.selenium.firefox.FirefoxProfile;
//
//import java.util.Collections;
//import java.util.HashMap;
//import java.util.Map;
//import java.util.concurrent.ArrayBlockingQueue;
//import java.util.concurrent.BlockingQueue;
//import java.util.concurrent.ConcurrentHashMap;
//
///**
// * @author: Sampson
// * @date: 2022-03-29 16:56
// */
//public class CrawlerWebDriverBuilder2 {
//
//    private static int maxWebDriverCount = 5;
//
//    Map<String,CrawlerWebDriver> webDriverMap = new ConcurrentHashMap<>();
//
//
//    private CrawlerWebDriver crawlerWebDriver;
//
//    private static BlockingQueue<Integer> webDriverBlockingQueue = new ArrayBlockingQueue<>(maxWebDriverCount);
//    private static BlockingQueue<Integer> webDriverBlockingWorkQueue = new ArrayBlockingQueue<>(maxWebDriverCount);
////    private static AtomicInteger aliveCount = new AtomicInteger();
//
//    public static CrawlerWebDriver buildFirefox() {
//        System.setProperty("webdriver.firefox.logfile", "/dev/null");
//        FirefoxProfile profile = new FirefoxProfile();
////        ProfilesIni allProfiles = new ProfilesIni();
////        FirefoxProfile profile = allProfiles.getProfile("NoJs");
//
////        profile.setPreference("javascript.enabled",false);
//        FirefoxOptions options = new FirefoxOptions();
//        options.setProfile(profile);
//        options.addArguments("--start-maximized");
////        options.addArguments("--disable-javascript");
//        options.addArguments("--disable-infobars");
//        options.addArguments("--disable-logging");
////        options.addArguments("headless");
//        options.addPreference("permissions.default.image", 2);
//        options.addPreference("permissions.default.stylesheet", 2);
////        options.addPreference("dom.ipc.plugins.enabled.libflashplayer.so",false);
//        options.addPreference("excludeSwitches", "enable-automation");
////            options.addPreference("excludeSwitches","--disable-logging");
////            options.addPreference("excludeSwitches"," enable-logging");
////            options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
//        FirefoxDriver driver = new FirefoxDriver(options);
//        driver.manage().window().maximize();
////            driver.setLogLevel(Level.OFF);
//        CrawlerWebDriver webDriver = new CrawlerWebDriver();
//        webDriver.setWebDriver(driver);
//        webDriver.setUse(false);
//        return webDriver;
//    }
//
//    public static CrawlerWebDriver buildChrome() {
//        ChromeOptions options = new ChromeOptions();
//        //去掉chrome 正受到自动测试软件的控制
////            options.addArguments("disable-infobars");
//
//        options.addArguments("--disable-infobars");
//        options.addArguments("--disable-extensions");
//        // 允许重定向
//        options.addArguments("--disable-web-security");
//        // 最大化
//        options.addArguments("--start-maximized");
//        options.addArguments("--no-sandbox");
//
//        //
////        options.addArguments("--headless");
//        options.addArguments("--disable-gpu");
//        options.addArguments("--no-sandbox");
//        options.addArguments("--allow-RUNNING-insecure-content'");  //设置proxy时添加的额外参数，允许不安全的证书
//        options.addArguments("--ignore-certificate-errors");  //设置proxy时添加的额外参数，允许不安全的证书
//        options.addArguments("user-agent='Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36'");
//
////            options.addArguments("--enable-automation");
//
////            options.setExperimentalOption("debuggerAddress","127.0.0.1:9222");
//
//        //开启开发者模式
//        options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
//
//        HashMap<String, Object> chromePrefs = new HashMap<>();
////        chromePrefs.put("permissions.default.stylesheet",2);
//        chromePrefs.put("profile.managed_default_content_settings.images", 2);
//        chromePrefs.put("profile.managed_default_content_settings.javascript", 2);
////        chromePrefs.put("profile.managed_default_content_settings.css",2);
//        chromePrefs.put("profile.managed_default_content_settings.stylesheet", 2);
////        chromePrefs.put("profile.managed_default_content_settings.permissions.default.stylesheet",2);
////        chromePrefs.put("profile.managed_default_content_settings.javascript",2);
//        options.setExperimentalOption("prefs", chromePrefs);
//
//        WebDriver driver = new ChromeDriver(options);
//        driver.manage().window().maximize();
//
//
//        CrawlerWebDriver webDriver = new CrawlerWebDriver();
//        webDriver.setWebDriver(driver);
//        webDriver.setUse(false);
//        return webDriver;
//    }
//
//    private static void preExecute(WebDriver driver) {
//        JavascriptExecutor driverJs = ((JavascriptExecutor) driver);
//        driverJs.executeScript("Object.defineProperty(navigator, 'webdriver', {\n" +
//                "      get: () => undefined\n" +
//                "    })");
//    }
//
//    private static void init(String type, int num) {
//        for (int i = 0; i < num; i++) {
//            CrawlerWebDriver webDriver;
//            if (StringUtils.equalsIgnoreCase(type, WebDriverTypeEnum.FIREFOX.getCode())) {
//                webDriver = buildFirefox();
//            } else {
//                webDriver = buildChrome();
//                webDriverBlockingQueue.add(webDriver);
//            }
//            webDriverBlockingQueue.add(webDriver);
////            webDriverBlockingFreeQueue.add(webDriver);
//        }
//    }
//
//
//    public static Object executeScript(WebDriver driver, String script) {
//        JavascriptExecutor driverJs = ((JavascriptExecutor) driver);
//        return driverJs.executeScript(script);
//    }
//
//
//    public static CrawlerWebDriver open(String type, String url) {
//        try {
//            synchronized (webDriverBlockingQueue) {
//                if (!webDriverBlockingWorkQueue.isEmpty()) {
//                    CrawlerWebDriver[] crawlerWebDrivers = webDriverBlockingWorkQueue.toArray(new CrawlerWebDriver[]{});
//                    for (CrawlerWebDriver crawlerWebDriver : crawlerWebDrivers) {
//                        //客户端已死掉的
//                        boolean dead = false;
//                        WebDriver webDriver = crawlerWebDriver.getWebDriver();
//                        try {
//                            webDriver.getWindowHandles();
//                        } catch (Exception e) {
//                            webDriverBlockingWorkQueue.remove(crawlerWebDriver);
//                            dead = true;
//                        }
//                        //客户端僵尸，没有及时回收到 webDriverBlockingQueue
//                        if (!dead) {
//                            int timeout = 5 * 60 * 1000;
//                            if (System.currentTimeMillis() - crawlerWebDriver.getUseTime() > timeout) {
//                                boolean state = webDriverBlockingWorkQueue.remove(crawlerWebDriver);
//                                if (state) {
//                                    webDriverBlockingQueue.add(crawlerWebDriver);
//                                }
//                            }
//
//                        }
//
//                    }
//                }
//                int newCount = maxWebDriverCount - (webDriverBlockingQueue.size() + webDriverBlockingWorkQueue.size());
//                if (newCount > 0) {
//                    init(type, newCount);
//                }
//            }
//            CrawlerWebDriver crawlerWebDriver = webDriverBlockingQueue.take();
////            aliveCount.incrementAndGet();
//            WebDriver webDriver = crawlerWebDriver.getWebDriver();
//            while (true) {
//                try {
//                    webDriver.getWindowHandles();
//                    break;
//                } catch (Exception e) {
////                    webDriverBlockingQueue.remove(crawlerWebDriver);
//                }
//                crawlerWebDriver = webDriverBlockingQueue.take();
////                aliveCount.incrementAndGet();
//                webDriver = crawlerWebDriver.getWebDriver();
//            }
//            webDriverBlockingWorkQueue.add(crawlerWebDriver);
//            if (crawlerWebDriver.isUse()) {
//                webDriver.navigate().to(url);
//            } else {
//                webDriver.get(url);
//                crawlerWebDriver.setUse(true);
//                crawlerWebDriver.setUseTime(System.currentTimeMillis());
//                if (StringUtils.equalsIgnoreCase(type, WebDriverTypeEnum.CHROME.getCode())) {
//                    preExecute(crawlerWebDriver.getWebDriver());
//                }
//            }
//            return crawlerWebDriver;
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        }
//    }
//
//    public static void scrollDown(WebDriver driver) {
//        JavascriptExecutor driverJs = ((JavascriptExecutor) driver);
//        try {
//            for (int i = 0; i < 50; i++) {
//                driverJs.executeScript("scrollTo(0," + (i * 100) + ")");
//                Thread.sleep(10);
//            }
//        } catch (Exception e) {
//        }
//    }
//
//    public static void close(CrawlerWebDriver crawlerWebDriver) {
//        if (crawlerWebDriver != null) {
//            boolean state = webDriverBlockingWorkQueue.remove(crawlerWebDriver);
//            if (state) {
//                webDriverBlockingQueue.add(crawlerWebDriver);
//            }
////            aliveCount.decrementAndGet();
//        }
//    }
//
//    public static void destory() {
//        CrawlerWebDriver crawlerWebDriver = webDriverBlockingQueue.poll();
//        while (crawlerWebDriver != null) {
//            crawlerWebDriver.getWebDriver().quit();
//
//        }
//    }
//}
